package service.impl.v2;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.CityVOMapper;
import model.CityVO;
import service.v2.CityService;
import util.ListUtil;
import util.PropertieUtil;
@Service
public class CityServiceImpl implements CityService {
	@Autowired
	private CityVOMapper dao;

	public List<CityVO> getTelCountryList() {
		return dao.getTelCountryList() ;
	}

	public int getCountryById(String country) {
		return dao.getCountryById(country) ;
	}

	public CityVO getCountryByAreaCode(String country) {
		return dao. getCountryByAreaCode(country);
	}

	public List<CityVO> getCityListAll() {
		return dao.getCityListAll();
	}

	public CityVO checkCityLevelAndId(String city) {
		return dao.checkCityLevelAndId(city);
	}
	
	@Override
	public CityVO getCityById(String country_id) {
		return dao.getCityById(country_id);
	}

	public List packageCountry(List<CityVO> cityList, String lang) {
		List l = new ArrayList();
		
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		//打包常用
		List<CityVO> list = new ArrayList<CityVO>();
		for (CityVO cityVO : cityList) {
//			if(lang == null || "cn".equals(lang)){
//				if("Y".equals(cityVO.getFreq())){
//					list.add(cityVO);
//				}
//			}else{
				if("Y".equals(cityVO.getFreq_en())){
					list.add(cityVO);
				}
//			}
		}
//		if(lang == null || "cn".equals(lang)){
//			new ListUtil<CityVO>().sort(list, "freq_ord", null);
//		}else{
			new ListUtil<CityVO>().sort(list, "freq_en_ord", null);
//		}
		map.put("countries", list);
		map.put("tag_name", PropertieUtil.loadLangPropertie(lang, "changyong"));
		l.add(map);
		String pinyin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (int i = 0; i < pinyin.length(); i++) {
			List<CityVO> pinyinList = new ArrayList<CityVO>();
			for (CityVO cityVO : cityList) {
//				if(lang == null || "cn".equals(lang)){
//					if(String.valueOf(pinyin.charAt(i)).equals(cityVO.getPinyin())){
//						pinyinList.add(cityVO);
//					}
//				}else{
					if(cityVO.getName_en().startsWith(String.valueOf(pinyin.charAt(i)))){
						pinyinList.add(cityVO);
					}
//				}
			}
			if(pinyinList.size() > 0){
				Map<String, Object> m = new LinkedHashMap<String, Object>();
				m.put("tag_name", String.valueOf(pinyin.charAt(i)));
				m.put("countries", pinyinList);
				l.add(m);
			}
		}
		
		return l;
	}

	public List packageCountryString(List<CityVO> cityList) {
		List<String> list = new ArrayList<String>();
		for (CityVO cityVO : cityList) {
			list.add(cityVO.getName()+"("+cityVO.getArea_code()+")");
		}
		return list;
	}

	@Override
	public List packageCountryList(List<CityVO> cityList) {
		List l = new ArrayList();
		CityVO vo =new CityVO();
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		//打包常用
		List<CityVO> list = new ArrayList<CityVO>();
		for (CityVO cityVO : cityList) {
			if("Y".equals(cityVO.getFreq())){
				list.add(cityVO);
			}
		}
		vo.setTag_name("常用");
		vo.setCountries(list);
		l.add(vo);
		String pinyin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (int i = 0; i < pinyin.length(); i++) {
			CityVO vo1 =new CityVO();
			List<CityVO> pinyinList = new ArrayList<CityVO>();
			for (CityVO cityVO : cityList) {
				if(String.valueOf(pinyin.charAt(i)).equals(cityVO.getPinyin())){
					pinyinList.add(cityVO);
				}
			}
			vo1.setTag_name(String.valueOf(pinyin.charAt(i)));
			vo1.setCountries(pinyinList);
			l.add(vo1);
		}
		return l;
	}

	@Override
	public List<CityVO> childCityList(String parent_id) {
		return dao.childCityList(parent_id);
	}
	@Override
	public CityVO checkInterCityLevelAndId(String inter_verify_country) {
		return dao.checkInterCityLevelAndId(inter_verify_country);
	}	
}
